import Vue from 'vue'
import VueRouter, { RouterLink } from 'vue-router'
import Home from "../views/main/main.vue";


// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 获取原型对象replace函数
const originalReplace = VueRouter.prototype.replace
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location){
return originalPush.call(this , location).catch(err=>err)
}
// 修改原型对象中的replace函数
VueRouter.prototype.replace = function replace(location){
return originalReplace.call(this , location).catch(err=>err)
}

Vue.use(VueRouter)
// "vue-router": "^3.5.1",
const routes = [
  {
    path: '/',
    redirect:'/controller ',
    component:()=>import('../views/main/main.vue'),
    children:[
      // {
      //   path:'datavisual',
      //   meta:{
      //     title:'数据展示'
      //   },
      //   component:()=>import('../views/datavisual/datavisual.vue')
      {
        path:'timestatistics',
        meta:{
          title:'用时统计'
        },
        component:()=>import('../views/timestatistics/timestatistics.vue')
      },
      {
        path:'operation',
        meta:{
          title:'运行概况'
        },
        component:()=>import('../views/operation/operation.vue')
      },
      {
        path:'actionlog',
        meta:{
          title:'操作日志'
        },
        component:()=>import('../views/actionlog/actionlog.vue')
      },
      {
        path:'profile',
        meta:{
          title:'情景模式'
        },
        component:()=>import('../views/profile/profile.vue')
      },
      // },
      {
        path:'statistic',
        meta:{
          title:'数据统计'
        },
        component:()=>import('../views/statistic/statistic.vue')
      },
      {
        path:'timecontrol',
        meta:{
          title:'设备时控'
        },
        component:()=>import('../views/timecontrol/timecontrol.vue')
      },
      {
        path:'controller',
        meta:{
          title:'集控器'
        },
        component:()=>import('../views/controller/controller.vue')
      },
      
    ]
  },
  
  {
    path:'/login',
    component:()=>import('../views/login/login.vue')
  },
]

const router = new VueRouter({
  // history模式，需要和服务端配合才能在生产环境下正常使用
  // mode: 'history',
  routes
})
// 全局前置守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){  //访问登录页
    next();
    return;
  }
  // 获取登录用户信息
  let userobj = localStorage['userInfo']?JSON.parse(localStorage['userInfo']):'';
  // let userPathArr =sessionStorage['usermenu']? JSON.parse(sessionStorage['usermenu']):[];
  // 判断用户是否登录
  if(userobj==''){
    next('/login')
    return;
  }
  else{
    next(); //有权限,放行
    // let allPathArr = ['/', '/login','/profile','/controller','/statistic','/timecontrol','/actionlog','/operation','/timestatistics',...userPathArr];
    // if (allPathArr.indexOf(to.path) == -1) {
    //   next('/login'); //没有权限,去登陆
    //   return;
    // } else {
    //   next(); //有权限,放行
    //   return;
    // }
  
  }
// 用户登录
  // let userPathArr =JSON.parse(sessionStorage['usermenu']);

  
 
})
// 全局前置路由
// 挂载路由导航守卫
// to 将要访问的路径
// from 从哪个路径跳转而来
// next 是个函数，表示放行 next() 放行  next('/login') 强制跳转

export default router
